﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using SiteServer.Plugin;
using Thingpower.SS.EduOnline.Model;

namespace Thingpower.SS.EduOnline.Provider
{
    public class UserStudyStatisticSEDAO : BaseDAO
    {
        public const string TableName = "view_UserStudyStatistic";

        
        #region 前台获取学员参与课程量排行榜
        public static List<UserStudyStatisticSEInfo> GetTopModelsList(int topNum)
        {
            var list = new List<UserStudyStatisticSEInfo>();

            string sqlString =
                $"SELECT TOP {topNum} UserId, UserName, CoursewaresCount FROM {TableName} ORDER BY {nameof(UserStudyStatisticSEInfo.CoursewaresCount)} DESC";

            using (var rdr = DataApi.ExecuteReader(ConnectionString, sqlString))
            {
                while (rdr.Read())
                {
                    list.Add(GetModel(rdr));
                }
                rdr.Close();
            }

            return list;
        }
        #endregion

        #region 获取指定学员参与课程量和浏览量的简化版信息
        private static UserStudyStatisticSEInfo GetModel(IDataRecord rdr)
        {
            if (rdr == null) return null;

            var _model = new UserStudyStatisticSEInfo();

            var i = 0;
            _model.UserId = rdr.IsDBNull(i) ? 0 : rdr.GetInt32(i);
            i++;
            _model.UserName = rdr.IsDBNull(i) ? string.Empty : rdr.GetString(i);
            i++;
            _model.CoursewaresCount = rdr.IsDBNull(i) ? 0 : rdr.GetInt32(i);
            return _model;
        }
        #endregion
    }
}